﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SolutionBLL;
using SolutionModel;
using SFast;

namespace MvcUI.Areas.AdminWeb.Controllers.Common
{
    public class CatalogController : Controller
    {
        public ViewResult Index()
        {
            return View();
        }


        #region Api: 获取分类 返回json
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns></returns>
        public JsonResult GetClass()
        {
            CommonClassType where = (CommonClassType)1;
            if (Request["type"] != null)
            {
                where = (CommonClassType)Enum.Parse(typeof(CommonClassType), Request["type"]);
            }
            String cid = String.Empty;
            if (!String.IsNullOrEmpty(Request["cid"]))
            {
                cid = Request["cid"];
            }

            CommonClassBLL bll = new CommonClassBLL();
            List<TreeGridModel> list = bll.GetListByType(where, cid);

            //是否添加根目录
            if (Request["root"] != null)
            {
                list = new List<TreeGridModel>(){
                new TreeGridModel(){
                    ID = 0,
                    PID = 0,
                    Name = where._GetRemark(),
                    CCType = where.ToString(),
                    children =list
                }
            };
            }
            //String JsonStr = list == null ? "" : list._ToJsonStr();
            //Response.Write(JsonStr);
            return Json(list);
        }
        #endregion

        #region Api:解析枚举 生成Json格式字符串
        /// <summary>
        /// 解析枚举 生成Json格式字符串
        /// </summary>
        /// <returns></returns>
        public JsonResult GetClassType()
        {
            List<object> list = new List<object>();
            Array Val = Enum.GetValues(typeof(CommonClassType));
            foreach (CommonClassType item in Val)
            {
                list.Add(new
                {
                    ID = item.ToString(),
                    Name = item._GetRemark()
                });
            }
            return Json(list);
        }
        #endregion

    }
}
